package ${sourceBasePackage}.service.impl<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.idp.common.enums.MessageEnum;
import com.idp.common.error.SystemException;
import com.idp.common.model.PageModel;
import ${sourceBasePackage}.dto<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Dto;
import ${sourceBasePackage}.dto<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}SearchDto;
import ${sourceBasePackage}.entity<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Entity;
import ${sourceBasePackage}.mapper<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Mapper;
import ${sourceBasePackage}.service<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ${description}Service实现类
 * @author ${author}
 */
@Service("${entityName?uncap_first}Service")
public class ${entityName}ServiceImpl implements ${entityName}Service {

	@Autowired
	private ${entityName}Mapper ${entityName?uncap_first}Mapper;

	@Override
	public PageModel<${entityName}Dto> findByPage(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {

		PageModel<${entityName}Dto> page = new PageModel<${entityName}Dto>();
		<#list searchFields as po>
			<#if po.listQuery == 'interval' && po.propertyType == 'Date'>
		if(ObjectUtil.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
			${entityName?uncap_first}SearchDto.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End()));
		}
			</#if>
		</#list>

		page.setList(${entityName?uncap_first}Mapper.getList(${entityName?uncap_first}SearchDto));

		return page;
	}

	@Override
	public ${entityName}Dto getById(${pkPropertyType} ${pkPropertyName}) {

		if(ObjectUtil.isEmpty(${pkPropertyName})) {
			throw new SystemException(MessageEnum.PARAM_ERROR);
		}

		${entityName}Dto ${entityName?uncap_first}Dto = ${entityName?uncap_first}Mapper.getById(${pkPropertyName});

		if(${entityName?uncap_first}Dto == null) {
			throw new SystemException(MessageEnum.DATA_NOT_EXIST);
		}

		return ${entityName?uncap_first}Dto;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(${entityName}Dto ${entityName?uncap_first}Dto) {

		${entityName}Entity ${entityName?uncap_first}Entity = BeanUtil.toBean(${entityName?uncap_first}Dto, ${entityName}Entity.class);

		${entityName?uncap_first}Mapper.insert(${entityName?uncap_first}Entity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(${entityName}Dto ${entityName?uncap_first}Dto) {

		${entityName}Entity ${entityName?uncap_first}Entity = ${entityName?uncap_first}Mapper.selectById(${entityName?uncap_first}Dto.get${pkPropertyName?cap_first}());

        if(${entityName?uncap_first}Entity == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }
		BeanUtil.copyProperties(${entityName?uncap_first}Dto, ${entityName?uncap_first}Entity, CopyOptions.create().setIgnoreNullValue(true));

		${entityName?uncap_first}Mapper.updateById(${entityName?uncap_first}Entity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(${pkPropertyType} ${pkPropertyName}) {

		${entityName}Entity ${entityName?uncap_first}Entity = ${entityName?uncap_first}Mapper.selectById(${pkPropertyName});

        if(${entityName?uncap_first}Entity == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }

		${entityName?uncap_first}Mapper.deleteById(${pkPropertyName});
	}

}
